package dao;

import java.util.List;
import model.Tablet;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

public class TabletDAO {
	private final SessionFactory sf = util.NewHibernateUtil.getSessionFactory();

	@SuppressWarnings("unchecked")
	public List<Tablet> showAllProduct() {
		try {
			sf.getCurrentSession().beginTransaction();
			return sf.getCurrentSession().createCriteria(Tablet.class).list();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Tablet> showAllMobileByAll(String key) {
		@SuppressWarnings("rawtypes")
		List tablet = sf
				.getCurrentSession()
				.createCriteria(Tablet.class)
				.add(Restrictions.or(Restrictions.like("id", "%" + key + "%"),
						Restrictions.or(Restrictions.like("name", "%" + key
								+ "%"), Restrictions.or(
								Restrictions.like("supplierName", "%" + key
										+ "%"),
								Restrictions.or(
										Restrictions.like("screen", "%" + key
												+ "%"),
										Restrictions.like("cpu", "%" + key
												+ "%")))))).list();

		return tablet;
	}
	@SuppressWarnings("unchecked")
	public List<Tablet> showAllMobileByInfor(String key) {
		@SuppressWarnings("rawtypes")
		List tablet = sf
		.getCurrentSession()
		.createCriteria(Tablet.class)
		.add(Restrictions.or(Restrictions.sqlRestriction("battery ="+key+""),
				Restrictions.or(
						Restrictions.sqlRestriction("camera ="+key+""),
						Restrictions.or(Restrictions.sqlRestriction("ram ="+key+""),
							Restrictions.or(Restrictions.sqlRestriction("purPrice ="+key+""),
									Restrictions.or(Restrictions.sqlRestriction("salPrice ="+key+""),
											Restrictions.sqlRestriction("quantity ="+key+"")
													)))))).list();

return tablet;
	}
	public boolean deleteTablet(Tablet product) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().delete(product);
			sf.getCurrentSession().getTransaction().commit();
			return true;
		} catch (Exception e2) {
			e2.printStackTrace();
			return false;
		}
	}
	
	public boolean updateProduct(Tablet c) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().saveOrUpdate(c);
			sf.getCurrentSession().getTransaction().commit();
			return true;

		} catch (Exception e2) {
			e2.printStackTrace();
			return false;
		}
	}
	public boolean insertProduct(Tablet c) {
		try {
			sf.getCurrentSession().beginTransaction();
			sf.getCurrentSession().save(c);
			sf.getCurrentSession().getTransaction().commit();
			return true;
		} catch (Exception em) {
			em.printStackTrace();
			sf.getCurrentSession().getTransaction().rollback();
			return false;
		}
	}
	public Tablet searchProduct(String id) {
		try {
			sf.getCurrentSession().beginTransaction();
			return (Tablet) sf.getCurrentSession().get(Tablet.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
